

class User{
    constructor(){
        this.session=sessionStorage.getItem("user")?JSON.parse(sessionStorage.getItem("user")):{};
        console.log(this.session);
        this.li=$("#list").children("li").eq(0);
        this.login=$("#login");
        this.register=$("#register");
        this.welcome=$("#welcome");
        this.init();
        this.addEvent();
    }
    init(){
        if(this.session.user){
            this.welcome.html( `${this.session.user} <span class="iconfont icon-xjt"></span>`).css({
                display:"inline",
                borderRight:"1px solid #666"
            }).siblings("a").css("display","none");
            
            this.li.hover(()=>{
                $(".second_list").css("display","flex");
            },()=>{
                $(".second_list").css("display","none");
            });
            $("#exit").on("click",()=>{
               sessionStorage.removeItem("user");
               this.welcome.html("").css({
                display:"none",
                borderRight:"0"
            }).siblings("a").css("display","inline");
            })
             
        }else{
            this.welcome.html("").css({
                display:"none",
                borderRight:"0"
            }).siblings("a").css("display","inline");


        }
    }
    addEvent(){
       this.login.on("click",function(){
            $(this).attr("href","login.html");
        })
        this.register.on("click",function(){
            $(this).attr("href","register.html");
        
        })
        
    }
}
new User();

$(".icon-gwc").on("click",function(){
    location.href="cart.html";
})
// 回到顶部
function back(){
    $(".back").on("click",function(){
        var top=document.documentElement.scrollTop;
        $("html").animate({
            scrollTop:0
        })
    })
}
back();

// 点击首页回到首页
function backIndex(){
    $(".header").children(".index").on("click",function(){
        location.href="../index.html";
    })
}
backIndex();

// 吸顶菜单的下拉分类

function classify(){
    $(".bigclass").hover(function(){
        $(".fullcont").css({
            display:"block"
        }).children(".bigcont").css("display","block")
    },function(){
        $(".fullcont").css({
            display:"none"
        }).children(".bigcont").css("display","none");
    })
    

    $(".bigcont").hover(function(){
        $(this).css("display","block");
    },function(){
        $(this).css("display","none");
    })


}
classify();
function list(){
     for(let i=0;i<$(".lb").length;i++){
        $(".lb").eq(i).hover(()=>{
            $(".biglist").css("display","block").children(".list_con"+i).css("display","block").siblings().css("display","none");
        })
     }

}
list();

document.onscroll=function(){
    if(document.documentElement.scrollTop>=470){
        $(".xd").css({
            display:"block",
            position:"fixed",
            top:"0"
        });
    }else{
        $(".xd").css({
            display:"none"
        });
    }
}

class requestData{
    constructor(){
        this.title=$(".list_detail").find($(".h3"));
        this.storage=localStorage.getItem("listContent")?  JSON.parse(localStorage.getItem("listContent")):{}
        this.init();
        
    }
    init(){
        if(this.storage.index){
            this.content=this.storage.content;
            this.title.html(this.content);

            console.log(this.storage.index);
            if(this.storage.index=="0"&&this.storage.content=="有品推荐"){
                this.ajax($(".xlbd"),"http://localhost:1000/data/list.json");
                this.ajax($(".mfrs"),"http://localhost:1000/data/list2.json");
            }

        

        }else{
            this.title.html("销量榜单");
        }
    }
    ajax(box,url){
        var that=this;
        $.ajax({
            url:url,
            success:function(res){
                that.res=res;
                var h2=$(`<h2>${res[0].h2}</h2>`);
                h2.prependTo(box);
                var str=``;
                for(var i=0;i<res.length;i++){
                    str+=`
                        <a class="list_sbox" href="detail.html?id=${res[i].id}" >
                            <img src="${res[i].img}" alt="" srcset="">
        
                            <p class="tit">${res[i].title}</p>
        
                            <p class="xq">${res[i].xq}</p>
                            <p class="tj"><span>${res[i].price}</span>
                                <i>${res[i].tj}</i></p>
                            </a>
                            `;
                }
                box.find(".listbox").html(str);

                for(var i=0;i<$(".tj").length;i++){
                    if($(".tj").eq(i).find("i").html()==""){
                        $(".tj").eq(i).find("i").css({
                            padding:0
                        })
                    }
                    
                }
        
            },
            error:function(xhr,status,res){
                console.log(res);
            }
        })
    }
    clickToDetail(){

    }
}
new requestData();



class clickToList{
    constructor(){
        this.aLi=$(".lb");
        this.a=this.aLi.children("a");
        this.aList=$(".biglist_con").children("ul");
        this.init();
        this.addEvent();
    }
    init(){
        // 给每个li加一个标志
        for(var i=0;i<this.aLi.length;i++){
            this.aLi.eq(i).attr("index",i);
        }
    }
    addEvent(){
        var that=this;
        for(var i=0;i<this.a.length;i++){
            this.a.eq(i).on("click",function(){
                that.index=$(this).parent().attr("index");
                that.aContent=$(this).html();
                that.storage={
                    "index":that.index,
                    "content":that.aContent
                }
                localStorage.setItem("listContent",JSON.stringify(that.storage));
                location.href="list.html";
            })
        }
         
    }

}
var c=new clickToList();
 
